﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArbolandiaBL
{
    internal class ContentManagementProvider
    {
        private ArbolandiaDBDataContext _db;
        private ImageProvider _imgprov;

        public ContentManagementProvider()
        {
            _db = new ArbolandiaDBDataContext();
            _imgprov = new ImageProvider();
        }

        internal void InsertContent(ContentManagement content)
        {
            var query = _db.ContentManagement.Where(w => w.Id == content.Id);
            if (query.Count() > 0)
            {
                query.First().Category = content.Category;
                query.First().Content = content.Content;
            }
            else
            {
                _db.ContentManagement.InsertOnSubmit(content);
            }
            _db.SubmitChanges();
        }
        internal void InsertContentPicture(int id, Picture picture)
        {

            //Guardamos la imagen y la damos de alta en arb_picture
            _imgprov.InsertUpdateImage(picture);

            //actualizamos el guid en la tabla de contentmanagement
            UpdatePictureGuidContent(id, picture.GuidId);
        }
        internal void DeleteContent(int id)
        {
            var query2 = from s in _db.ContentManagement
                         join p in _db.Picture on s.PicGuidId equals p.GuidId
                         where s.Id == id
                         select p;
            if (query2.Count() > 0)
            {
                _db.Picture.DeleteOnSubmit(query2.First());
                _db.SubmitChanges();
            }

            var query = _db.ContentManagement.Where(w => w.Id == id);
            if (query.Count() > 0) { _db.ContentManagement.DeleteOnSubmit(query.First()); _db.SubmitChanges(); }
        }
        internal List<ContentManagement> GetContents(int id)
        {
            var res = new List<ContentManagement>();

            var query = CompiledQuerys.GetContentManagements(_db, id);
            foreach (var p in query)
            {
                res.Add(new ContentManagement
                {
                    Id = p.Id,
                    Category = p.Category,
                    Content = p.Content,
                    UrlImage = p.UrlImage
                });
            }
            return res;
        }
        internal ContentManagement GetContent(int id)
        {
            var res = new List<ContentManagement>();
            if (id > 0)
            {
                var query = CompiledQuerys.GetContentManagements(_db, id);
                foreach (var p in query)
                {
                    res.Add(new ContentManagement
                    {
                        Id = p.Id,
                        Category = p.Category,
                        Content = p.Content,
                        UrlImage = p.UrlImage
                    });
                }
            }
            return res.FirstOrDefault<ContentManagement>();

        }
        internal void DeletePictureContent(int id)
        {
            var query = from s in _db.ContentManagement
                        join p in _db.Picture on s.PicGuidId equals p.GuidId
                        where s.Id == id
                        select p;
            if (query.Count() > 0)
            {
                _db.Picture.DeleteOnSubmit(query.First());
                _db.SubmitChanges();
            }
            var query2 = _db.ContentManagement.Where(w => w.Id == id);
            if (query2.Count() > 0)
            {
                query2.First().PicGuidId = null;
                _db.SubmitChanges();
            }
        }
        internal void UpdatePictureGuidContent(int id, Guid guid)
        {
            var query = _db.ContentManagement.Where(w => w.Id == id);
            if (query.Count() > 0)
            {
                query.First().PicGuidId = guid;
                _db.SubmitChanges();
            }
        }

    }
}
